package cc.lj.internal.coupons.application.service;


import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapMerchantDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.MartCapDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.MultipleModelDO;
import cc.lj.internal.coupons.domain.martcap.service.ICapMerchantDomainService;
import cc.lj.internal.coupons.domain.martcap.service.IMartCapDomainService;
import cc.lj.internal.coupons.domain.martcap.service.IMultipleModelDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.interfaces.dto.MultipleModelDTO;
import cc.lj.internal.coupons.interfaces.dto.MultipleModelListDTO;
import cc.lj.internal.coupons.interfaces.dto.command.MultipleModelUpdateCommand;
import cc.lj.internal.coupons.interfaces.dto.convert.MultipleModelDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.query.MultipleModelListQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 倍数表应用层逻辑
 */
@Service
@Slf4j
public class MultipleModelAppService {

    @Autowired
    private IMultipleModelDomainService multipleModelDomainService;
    @Autowired
    private ICapMerchantDomainService capMerchantDomainService;

    /**
     * 倍数模型返回组合列表，包含该倍数模型下商户封顶额总和
     * @param multipleModelListQuery
     * @return
     */
    public List<MultipleModelListDTO> getList(MultipleModelListQuery multipleModelListQuery) {
        //查询倍数模型
        List<MultipleModelDO> list = multipleModelDomainService.getList(MultipleModelDTOConvertDO.INSTANCE.convert(multipleModelListQuery));

        List<MultipleModelListDTO> return_list = new ArrayList<>();
        //查询倍数模型对应的所有商户封顶额的总和
        for (MultipleModelDO item:list) {
            MultipleModelListDTO listDTO = new MultipleModelListDTO();
            listDTO.setCapId(item.getCapId());
            listDTO.setDayNum(item.getDayNum());
            listDTO.setId(item.getId());
            listDTO.setMultipleNum(item.getMultipleNum());
            listDTO.setModelValue(item.getModelValue());
            listDTO.setCreateTime(item.getCreateTime());
            listDTO.setUpdateTime(item.getUpdateTime());

            //查询封顶额总和
            CapMerchantDO capMerchantDO = new CapMerchantDO();
            capMerchantDO.setModelId(item.getId());
            Integer totalValue = capMerchantDomainService.getValueSum(capMerchantDO);

            listDTO.setTotalValue(totalValue);
            return_list.add(listDTO);
        }
        return return_list;
    }
}
